﻿Public Class UtilDB

    Public Shared Function Criptografar(ByVal parTexto As String, Optional ByVal parNovo As Boolean = False) As String
        Dim temp As String = "", tempA As String, rand As String, rad As String, crntASC As String
        Dim i As Integer
100:    Randomize()
        rand = Right(Rnd, 3)
        rad = Left(rand, 1)
        If Left(rand, 1) = "-" Then
            GoTo 100
        End If
        For i = 1 To Len(parTexto)
            crntASC = Asc(Mid(parTexto, i, 1))
            tempA = ((crntASC) Xor (rand + i + rad)) + (i + rad)
            If Len(tempA) = 4 Then
                temp = temp & tempA
            ElseIf Len(tempA) = 3 Then
                temp = temp & "0" & tempA
            ElseIf Len(tempA) = 2 Then
                temp = temp & "00" & tempA
            ElseIf Len(tempA) = 1 Then
                temp = temp & "000" & tempA
            End If
        Next i
        If parNovo Then
            i = Len(temp) / 2
            temp = temp.Substring(0, i) & rand & temp.Substring(i)
        Else
            temp = rand & temp
        End If

        Criptografar = temp
    End Function



    Public Shared Function Descriptografar(ByVal parTexto As String, Optional ByVal parNovo As Boolean = False) As String
        Try
            Dim sTemp As String, sTemp1 As String, sTemp2 As String = ""
            Dim sCont1 As Integer, sCont2 As Integer
            Descriptografar = ""
            parTexto = Trim(parTexto)
            If parTexto.Length > 3 Then
                If parNovo Then
                    sCont1 = (Len(parTexto) - 3) / 2
                    sTemp = parTexto.Substring(sCont1, 3)
                    parTexto = parTexto.Substring(0, sCont1) & parTexto.Substring(sCont1 + 3)
                Else
                    sTemp = Left(parTexto, 3)
                    parTexto = parTexto.Substring(3)
                End If
                For sCont1 = 1 To Len(parTexto) Step 4
                    sCont2 = sCont2 + 1
                    sTemp1 = Mid(parTexto, sCont1, 4)
                    sTemp1 = ((sTemp1 - (sCont2 + Left(sTemp, 1))) Xor (sTemp + sCont2 + Left(sTemp, 1)))
                    sTemp2 = sTemp2 & Chr(sTemp1)
                Next sCont1
            End If
            Return sTemp2
        Catch ex As Exception
            Return ""
        End Try
    End Function



End Class
